﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;

namespace ncp.Web
{
    public partial class forget_passwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(forget_passwd));
        }

        //验证验证码是否正确
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string VerificationCode(string code)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            string verCode = Session["VerificationCode"] == null ? "" : Session["VerificationCode"].ToString();
            if (verCode.ToLower() == code.ToLower())
            {
                jsonResult.Add("isreload", true);
            }
            else
            {
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string SendEmail(string code)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                Dictionary<string, object> dicWhere=new Dictionary<string, object>();
                dicWhere.Add("Client_Account",code);
                var dt = ClientInfoBll.GetClientInfoTable("*", dicWhere, CommonConst.DB_Name);
                if (dt.Rows.Count==0)
                {
                    jsonResult.Add("isreload",false);
                    jsonResult.Add("msg","用户名不存在，请填写正确的用户名！");
                }
                else
                {
                    var email = dt.Rows[0]["User_Email"];
                    var password = EncryptHelper.DecryptString(dt.Rows[0]["User_Password"].ToString());
                    if (email==DBNull.Value||email.ToString()=="")
                    {
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "用户邮箱地址不正确！");
                    }
                    else
                    {
                        string content = "亲爱的 " + code + " 用户您好：您的密码为:<b>" + password + "</b><br/><br/>中国农业信息化服务网";
                        string returnValue= CommonBll.SendEmail(content, email.ToString(), "", CommonConst.DB_Name);
                        if (returnValue=="OK")
                        {
                            jsonResult.Add("isreload", true);
                            jsonResult.Add("msg", "已成功将密码发送到您邮箱中，注意查收！");
                        }
                        else
                        {
                            jsonResult.Add("isreload", false);
                            jsonResult.Add("msg", returnValue);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                jsonResult.Clear();
                jsonResult.Add("isreload",false);
                jsonResult.Add("msg",ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
